//
//  HZL_StoryAllPageViewController.m
//  B_Project
//
//  Created by 胡佳俊 on 15/11/28.
//  Copyright © 2015年 HZL. All rights reserved.
//

#import "HZL_StoryAllPageViewController.h"
#import "HZL_StoryItemCollectionViewCell.h"
#import "HZL_StoryDetailPageViewController.h"
#import "MJRefresh.h"

@interface HZL_StoryAllPageViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)NSMutableArray *modelDataArr; //总数据数组
@property (nonatomic, retain)NSMutableArray *modelUserDataArr; //用户信息数组
@property (nonatomic, assign)NSInteger startID; //开始ID

@end

@implementation HZL_StoryAllPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self getData];
    [self createView];
    [self loadMBProgress];
}
#pragma mark - 获取数据
- (void)getData
{
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=0"];
    [HZL_NetworkingHandle getUrl:str cookie:nil result:^(id result) {
        NSMutableDictionary *dic = result;
        self.modelDataArr = [HZL_RecommendModel transformWithArray:dic[@"data"][@"hot_spot_list"]];
        NSMutableArray *userDataArr = [NSMutableArray array];
        for (NSDictionary *tempDic in dic[@"data"][@"hot_spot_list"]) {
            [userDataArr addObject:tempDic[@"user"]];
        }
        self.modelUserDataArr = [HZL_RecommendModel transformWithArray:userDataArr];
        [self.hud removeFromSuperview];
        [self.collectionView reloadData];
        
    }];
    
}

- (void)createView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(( [UIScreen mainScreen].bounds.size.width - 30) / 2, 250);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    self.collectionView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[HZL_StoryItemCollectionViewCell class] forCellWithReuseIdentifier:@"item"];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;

    [self MJRefresh];
}
- (void)MJRefresh
{
    self.startID = 12;
    self.collectionView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=%ld", self.startID];
        [HZL_NetworkingHandle getUrl:str cookie:nil result:^(id result) {
            
            NSMutableDictionary *dic = result;
            
            self.startID += 12;
            
            [self.modelDataArr addObjectsFromArray: [HZL_RecommendModel transformWithArray:dic[@"data"][@"hot_spot_list"]]];
            
            
            NSMutableArray *userDataArr = [NSMutableArray array];
            for (NSDictionary *tempDic in dic[@"data"][@"hot_spot_list"]) {
                [userDataArr addObject:tempDic[@"user"]];
            }
            
            [self.modelUserDataArr addObjectsFromArray: [HZL_RecommendModel transformWithArray:userDataArr]];
            [self.collectionView.mj_footer endRefreshing];
            [self.collectionView reloadData];
            
        }];
    }];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.modelDataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HZL_StoryItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    cell.storyAllModel = self.modelDataArr[indexPath.item];
    cell.userModel = self.modelUserDataArr[indexPath.item];
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    HZL_StoryDetailPageViewController *storyDetail = [[HZL_StoryDetailPageViewController alloc]init];
    storyDetail.spot_id = ((HZL_RecommendModel *)self.modelDataArr[indexPath.item]).spot_id;
    storyDetail.hidesBottomBarWhenPushed = YES;
    //收藏
    storyDetail.recommendModel = self.modelDataArr[indexPath.item];
    storyDetail.userModel = self.modelUserDataArr[indexPath.item];
    [self.navigationController pushViewController:storyDetail animated:YES];

    
}
#pragma mark-滑动手势1
- (void)addScreenEdgePan
{
    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(presentLeftMenuViewController:)];
    screenEdgePan.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:screenEdgePan];
    [self.collectionView addGestureRecognizer:screenEdgePan];
}
#pragma mark-滑动手势2
-(void)presentLeftMenuViewController:(UIScreenEdgePanGestureRecognizer *)edge
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
